//
//  BHChosePlaceHeaderView.swift
//  BigHealth
//
//  Created by 蔺百好 on 2021/6/25.
//

import UIKit

class BHChosePlaceHeaderView: UIView {

    var changeToIndexBlock:((Int) -> Void)?
    var goSearchingBlock:((String) -> Void)?
    
    
    @IBOutlet weak var searchBackView: UIView!
    
    @IBOutlet weak var searchTextFiled: UITextField!
    
    @IBOutlet weak var sportPlaceBtn: UIButton!
    
    @IBOutlet weak var medicalPlaceBtn: UIButton!
    
    
    static func loadFromXib() -> BHChosePlaceHeaderView {
        let viewArray = Bundle.main.loadNibNamed("BHChosePlaceHeaderView", owner: nil, options: nil)
        let view: BHChosePlaceHeaderView = viewArray![0] as! BHChosePlaceHeaderView
        return view
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        self.searchBackView.addLayerCorner(16, 0, .white)
        
        self.searchTextFiled.delegate = self
       
    }
    
    
    @IBAction func headerBtnAct(_ sender: UIButton) {
        if sender.isSelected == true {
            return
        }
        
        sender.isSelected = true
        if sender == self.sportPlaceBtn {
            self.medicalPlaceBtn.isSelected = false
            self.changeToIndexBlock?(1)
        }else {
            self.sportPlaceBtn.isSelected = false
            self.changeToIndexBlock?(2)
        }
    }
    
    static var bottomHeight: CGFloat {
        get {
            return 95
        }
    }
    
    
}

extension BHChosePlaceHeaderView: UITextFieldDelegate {
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }
    func textFieldDidEndEditing(_ textField: UITextField) {
        self.goSearchingBlock?(textField.text ?? "")
    }
}
